package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.AdviceThumbsUp;
import com.dnjn.happiness.biz.service.IAdviceService;
import com.dnjn.happiness.biz.service.IAdviceThumbsUpService;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 点赞Controller
 *
 * @author happiness
 * @date 2023-10-16
 */
@Tag(name = "biz up 点赞", description = "点赞")
@RestController
@RequestMapping("/up")
public class AdviceThumbsUpController extends BaseController {
    @Autowired
    IAdviceService adviceService;
    @Autowired
    private IAdviceThumbsUpService adviceThumbsUpService;

    /**
     * 查询点赞列表
     */
    @RequiresPermissions("biz:up:list")
    @PostMapping("/list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody AdviceThumbsUp adviceThumbsUp, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<AdviceThumbsUp> list = adviceThumbsUpService.page(page, Wrappers.query(adviceThumbsUp));
        return success(list);
    }

    /**
     * 导出点赞列表
     */
    @Log(title = "点赞", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:up:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdviceThumbsUp adviceThumbsUp) {
        List<AdviceThumbsUp> list = adviceThumbsUpService.list(Wrappers.query(adviceThumbsUp));
        ExcelUtil<AdviceThumbsUp> util = new ExcelUtil<AdviceThumbsUp>(AdviceThumbsUp.class);
        util.exportExcel(response, list, "点赞数据");
    }

    /**
     * 获取点赞详细信息
     */
    @RequiresPermissions("biz:up:query")
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(adviceThumbsUpService.getById(id));
    }



    /**
     * 修改点赞
     */
    @Log(title = "点赞", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:up:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody AdviceThumbsUp adviceThumbsUp) {
        return toAjax(adviceThumbsUpService.updateById(adviceThumbsUp));
    }

    /**
     * 删除点赞
     */
    @Log(title = "点赞", businessType = BusinessType.DELETE)
    @RequiresPermissions("biz:up:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(adviceThumbsUpService.removeBatchByIds(Arrays.asList(ids)));
    }


}
